import { defineStore } from "pinia";
import { ref, type App } from "vue";
import { useRoute } from "vue-router";



export const usePermission = defineStore('permission', () => {
    const isAdmin = ref(false);
    const actions = ref<Record<string, string[]>>({
        'home': []
    });
    return {
        ActionMap: actions,
        hasPermission: (key: string) => {
            const route = useRoute();
            if (isAdmin.value) {
                return true;
            } else {
                return actions.value[route.path]?.includes(key)
            }
        },
        setAdmin: (val: boolean) => {
            isAdmin.value = val;
        }
    }
})

export default {
    install(app: App) {
        const permission = usePermission();
        app.config.globalProperties.$p = permission.hasPermission;
    }
}